package com.xuexiang.xuidemo.slice.imageview.pictureselector;


import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.VectorElement;

import com.xuexiang.xuidemo.ResourceTable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author XUE
 * @since 2019/3/25 11:29
 */
public class ImageSelectGridAdapter extends BaseItemProvider {
    public static final int TYPE_CAMERA = 1;
    public static final int TYPE_PICTURE = 2;
    //private LayoutScatter mInflater;
    private List<LocalMedia> mList = new ArrayList<>();
    private int mSelectMax = 9;
    private AbilitySlice mContext;
    public final static int TYPE_AUDIO = 3;
    /**
     * 点击添加图片跳转
     */
    private OnAddPicClickListener mOnAddPicClickListener;

    public interface OnAddPicClickListener {
        void onAddPicClick();
    }

    public ImageSelectGridAdapter(AbilitySlice context, OnAddPicClickListener onAddPicClickListener) {
        mContext = context;
        //mInflater = LayoutScatter.getInstance(context);
        mOnAddPicClickListener = onAddPicClickListener;
    }

    public void setSelectMax(int selectMax) {
        this.mSelectMax = selectMax;
    }

    public void setSelectList(List<LocalMedia> list) {
        mList = list;
    }

    public void update(List<LocalMedia> list) {
        mList = list;
        notifyDataChanged();
    }

    public int getItemViewType(int position) {
        if (isShowAddItem(position)) {
            return TYPE_CAMERA;
        } else {
            return TYPE_PICTURE;
        }
    }


    private boolean isShowAddItem(int position) {
        int size = mList.size();
        return position == size;
    }

    @Override
    public int getCount() {
        if (mList.size() < mSelectMax) {
            return mList.size() + 1;
        } else {
            return mList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return Optional.empty();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ViewHolder viewHolder;
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_adapter_select_image_grid_item, null, false);
            viewHolder = new ViewHolder(cpt);
            cpt.setTag(viewHolder);
        } else {
            cpt = component;
            // 从缓存中获取到列表项实例后，直接使用绑定的子组件信息进行数据填充。
            viewHolder = (ViewHolder) cpt.getTag();
        }
        //少于8张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_CAMERA) {
            viewHolder.ivSelectPic.setImageElement(new VectorElement(mContext, ResourceTable.Graphic_ic_add_image));
            viewHolder.ivSelectPic.setClickedListener(v -> mOnAddPicClickListener.onAddPicClick());
            viewHolder.llDelete.setVisibility(Component.INVISIBLE);
        }  /*  else {
            viewHolder.llDelete.setVisibility(Component.VISIBLE);
            viewHolder.llDelete.setClickedListener(view -> {
              //  int index = viewHolder.getAdapterPosition();
                int index=position;
                // 这里有时会返回-1造成数据下标越界,具体可参考getAdapterPosition()源码，
                // 通过源码分析应该是bindViewHolder()暂未绘制完成导致，知道原因的也可联系我~感谢
                if (index != ListContainer.INVALID_INDEX) {
                    mList.remove(index);
                    notifyDataSetItemRemoved(index);
                    notifyDataSetItemRangeChanged(index, mList.size());
                }
            });
            LocalMedia media = mList.get(position);
            int mimeType = media.getMimeType();
            String path;
            if (media.isCut() && !media.isCompressed()) {
                // 裁剪过
                path = media.getCutPath();
            } else if (media.isCompressed() || (media.isCut() && media.isCompressed())) {
                // 压缩过,或者裁剪同时压缩过,以最终压缩过图片为准
                path = media.getCompressPath();
            } else {
                // 原图
                path = media.getPath();
            }
            if (mimeType == TYPE_AUDIO) {
                //viewHolder.ivSelectPic.setImageResource(R.drawable.pic_audio_placeholder);
            } else {
       *//*         RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.color.color_f4)
                    .diskCacheStrategy(DiskCacheStrategy.ALL);
                Glide.with(viewHolder.itemView.getContext())
                    .load(path)
                    .apply(options)
                    .into(viewHolder.ivSelectPic);*//*
            }
            //itemView 的点击事件
            if (mItemClickListener != null) {
                viewHolder.itemView.setClickedListener(v-> {
                    int adapterPosition = position;
                    mItemClickListener.onItemClick(adapterPosition, v);
                });
            }
        }*/


        return cpt;
    }

    public static class ViewHolder {

        Image ivSelectPic;
        DirectionalLayout llDelete;
        //Component itemView;

        public ViewHolder(Component view) {
            //itemView = view;
            ivSelectPic = (Image) view.findComponentById(ResourceTable.Id_iv_select_pic);
            llDelete = (DirectionalLayout) view.findComponentById(ResourceTable.Id_ll_delete);
        }
    }

    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, Component v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }
}
